﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace Miao.Stand
{
    public class SyncHediff : HediffWithComps
    {
        private HediffStage hediffStage = new HediffStage();
        public StandPawn standPawn
        {
            get
            {
                return pawn as StandPawn;
            }
        }
        public SyncHediff()
        {
            hediffStage.becomeVisible = false;
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Consciousness });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Sight });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Hearing });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Moving });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Manipulation });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.Talking });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.BloodFiltration });
            hediffStage.capMods.Add(new PawnCapacityModifier() { capacity = PawnCapacityDefOf.BloodPumping });

            //PawnCapacityModifier pawnCapacityModifier = new PawnCapacityModifier();
            //pawnCapacityModifier.capacity = PawnCapacityDefOf.Consciousness;
            //this.CapMods.Add(pawnCapacityModifier);
        }


        public override void Tick()
        {
            base.Tick();

            if (Find.TickManager.TicksGame % 250 == 0)
            {
                RefreshCapacity();
            }
        }

        public override HediffStage CurStage
        {
            get
            {
                return hediffStage;
            }
        }
        

        //我懂为什么有问题了，这个获取的capacity值是全替身使者共享的
        public void RefreshCapacity()
        {
            for (int i = 0; i < this.CapMods.Count; i++)//叠加计算的情况下就算不出值了，意识会减所有
            {
                float level = standPawn.owner.health.capacities.GetLevel(this.CapMods[i].capacity);
                if (this.CapMods[i].capacity == PawnCapacityDefOf.Consciousness)//因为意识会减所有，可能会造成冲突，所以意识排除在减值外
                {
                    //这段是给替身意识长度，用来决定召唤替身行动范围的
                    standPawn.Consciousness = level;
                }
                this.CapMods[i].offset = level - 1;
            }
        }
        public override void ExposeData()
        {
            //if (Scribe.mode == LoadSaveMode.Saving)
            //{
            //    for (int i = 0; i < this.CapMods.Count; i++)//存储前将数值还原，避免污染数值
            //    {
            //        this.CapMods[i].offset = 0;
            //    }
            //}
            if (Scribe.mode == LoadSaveMode.ResolvingCrossRefs)
            {
                standPawn.Consciousness = standPawn.health.capacities.GetLevel(PawnCapacityDefOf.Consciousness);
            }
            base.ExposeData();
        }

    }
}
